package com.GroupSix.controller;

import com.GroupSix.entity.WrongQuestionEntity;
import com.GroupSix.service.QuestionBankService;
import com.GroupSix.service.WrongQuestionService;
import com.GroupSix.vo.WrongPracticVo;
import com.GroupSix.vo.WrongQuestionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("backend/WrongQuestion")
public class WrongQuestionController {
    @Autowired
    private WrongQuestionService wrongQuestionService;
    private QuestionBankService questionBankService;

    @PostMapping("/add")
    public String addWrongQuestion(
           @RequestBody WrongQuestionEntity entity) {
        // 备注是可选的
        if(null == entity.getComment()){
            entity.setComment("");
        }
        boolean isAdded = wrongQuestionService.addWrongQuestionManually(entity.getUserId(), entity.getQuestionId(), entity.getComment());
        return isAdded ? "错题添加成功！" : "错题记录已存在，无法重复添加！";
    }

    @DeleteMapping("/delete")
    public String deleteWrongQuestion(@RequestBody WrongQuestionEntity entity) {
        if (null == entity.getUserId() || null == entity.getQuestionId()) {
            return "删除失败，缺少必要参数！";
        }
        boolean isDeleted = wrongQuestionService.deleteWrongQuestion(entity.getUserId(), entity.getQuestionId());
        return isDeleted ? "删除错题成功！" : "删除失败，记录不存在！";

    }


    @PutMapping("/update-comment")
    public String updateComment(@RequestBody WrongQuestionEntity entity) {
        boolean isUpdated = wrongQuestionService.updateComment(entity.getUserId(), entity.getQuestionId(), entity.getComment());
        return isUpdated ? "解析内容更新成功！" : "更新失败，记录不存在！";
    }

    @GetMapping("/allWrong")
    public List<WrongQuestionVo> getWrongQuestionsByUserId(@RequestBody WrongQuestionEntity entity) {
        return wrongQuestionService.getWrongQuestionsByUserId(entity.getUserId(),entity.getQscourse());
    }

    /**
     * 查询某位同学的错题，并按指定字段排序
     *
     * @param entity
     * @return 错题列表
     */
    @GetMapping("/allWrongOrder")
    public List<WrongQuestionVo> getWrongQuestionsSortedByField( @RequestBody WrongQuestionEntity entity) {
        // 调用 Service 方法获取结果
        return wrongQuestionService.getWrongQuestionsSortedByField(entity.getUserId(), entity.getOrderBy(), entity.getOrderDirection(),entity.getQscourse());
    }

    /**
     * 随机抽取错题本作为练习题
     *
     * @param entity （num 题目数量 qscourse 科目 userId 学生ID）
     * @return 错题列表
     */
    @GetMapping("/randWrongByNum")
    public List<WrongPracticVo> randWrongByNum(@RequestBody WrongQuestionEntity entity) {
        // 调用 Service 方法获取结果
        return wrongQuestionService.randWrongByNum(entity.getUserId(), entity.getNum(),entity.getQscourse());
    }


}






